import { Tool } from '../../../Tool';
import VGE from '../../../VGE';
/**
 * 构件属性查询
 */
export class ComponentQueryTool extends Tool {
	constructor(viewer, ...args) {
		super(arguments);
		this.viewer = viewer;
		this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
		[...arguments].forEach((item) => {
			if (typeof item === 'function') {
				this.callback = item;
			}
		});
		this.callbackValue = {
			total: null,
			current: null,
			tool: this
		};
	}

	start() {
		this.handler.setInputAction((e) => {
			const pick = this.viewer.scene.pick(e.position);
			if (!pick) return;
			const layer = pick?.primitive;
			const dataUrl = layer._subdomainsUrlScheme
				.replace(/3D/, 'data')
				.replace(/\/realspace$/, '/data');
			VGE.request.get(dataUrl + '/datasources.rjson').then(({ data }) => {
				const [datasourceName] = data.datasourceNames;
				VGE.request
					.get(`${dataUrl}/datasources/${datasourceName}/datasets.rjson`)
					.then(({ data }) => {
						const [datasetName] = data.datasetNames;
						const url = `${dataUrl}/datasources/${datasourceName}/datasets/${datasetName}/features/${pick.id}.rjson?hasGeomentry=false`;
						VGE.request.get(url).then(({ data }) => {
							this.callbackValue.current = data;
							this.callback && this.callback(this.callbackValue);
							console.info(data);
						});
					});
			});
		}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
	}

	cancel() {
		this.handler?.destroy();
	}
}
ComponentQueryTool.toolId = 'ComponentQueryTool';
